3.5.2. Connecting from the Entity Framework
This example uses the ADO.NET Entity Framework and lets it handle your connection. Follow these steps:
Fire
up Visual Studio 2010, and create a new Windows Forms Application
project. For this simple example, you can use whatever project name you
like.
Right-click
the project in the Solution Explorer window, and select Add → New Item
from the context menu. Doing so brings up the Add New Item dialog.
Select the ADO.NET Entity Data Model data template, as shown in Figure 2.
Accept the default name of Model1 and click the Add button, which starts the Entity Data Model Wizard.
The
first page of wizard is the Choose Model Contents page, which lets you
create a conceptual model of your database by reverse-engineering an
existing database. Or, you can start with an empty model and build your
conceptual model by hand first. For this example, select the Generate
From Database option, and click Next.
NOTE
This section doesn't go deep into the ADO.NET Entity Framework. For more information about the Entity Framework, see Pro ADO.NET 4.0 Entity Framework by Scott Klein (Apress, 2010).
Next
is the Choose Your Data Connection page. You need to create a
connection to your new EFAzure database, so click the New Connection
button. Doing so brings up the Connection Properties dialog, which
you've probably seen before; it lets you define a connection that your
application uses to connect to the specified database.
In the Connection Properties dialog, click the Change button to open the Choose Data Source dialog shown in Figure 3.
Select Microsoft SQL Server from the list of data sources, and click Continue to return to the Connection Properties dialog.
Enter the server name and the administrator username and password, and select the EFAzure database (see Figure 4).
The server name is the logical name of the server (FQDN); you can get
that piece of information from the SQL Azure Portal in the Server
Information section.
To
ensure that your connection works, click the Test Connection button. If
you entered everything correctly and the firewall settings are correct,
you get a message stating that the test connection succeeded.
Click
OK in the Connection Properties dialog. Doing so takes you back to the
Entity Data Model Wizard's Choose Your Data Connection page.
Your
connection has been defined, and you're ready to proceed. You may need
to select whether to include the sensitive data in the connection string
or to exclude it. For the sake of this example, select the include
option (see Figure 5). Keep the other default settings, and click Next.
The
next step of the Entity Data Model Wizard allows you to select the
objects from your database that you want to include in your conceptual
model (see Figure 6).
You see nodes for Views and Stored Procedures; but because you didn't
create any, you can't select anything. Select the Contact table, and
click Finish.
The
Entity Data Model Wizard creates your conceptual model and displays it
in the Visual Studio IDE. You won't work with this directly, so close
it. You do need the form, so open it in Design view and place a ListBox
on it, leaving the default name listbox1.
Double-click the form (not the ListBox) to display the code-behind and create the form's Load
event. You want to do something very simple: load the contacts from the
Contact table into the list box when the form loads. (In normal
circumstances, this wouldn't be a good idea; but because the table
contains only a few contacts, and this example is demonstrating the
functionality of querying an Azure database, you can let it slide.) In
the Load event, enter the following code:
using (EFAzureEntities context = new EFAzureEntities())
{
var query = from con in context.Contacts
select con;
foreach (var cont in query)
{
listBox1.Items.Add(cont.FirstName);
}
}
The first line creates an instance of the EFAzureEntities
class. This class lets you work with database objects in terms of .NET
object-oriented objects. You then use the Language Integrated Query
(LINQ) language technology (in this case, LINQ to Entities) to query the
Contact table and fill the list box with the first names of all the
contacts, as shown in Figure 7.
And there you have it; you've
successfully queried the cloud. Yes, this is a simple example, but its
purpose is to illustrate how easy it is to connect to SQL Azure, create
and populate a database, and create an application that queries the
database.